(0, require("../common/component").VantComponent)({
    relation: {
        name: "badge",
        type: "descendant",
        linked: function(badgeComponent) {
            this.badges.push(badgeComponent), this.setActive(this.data.active);
        },
        unlinked: function(badgeComponent) {
            this.badges = this.badges.filter(function(currentBadge) {
                return currentBadge !== badgeComponent;
            }), this.setActive(this.data.active);
        }
    },
    props: {
        active: {
            type: Number,
            value: 0,
            observer: "setActive"
        }
    },
    beforeCreate: function() {
        this.badges = [], this.currentActiveIndex = -1;
    },
    methods: {
        setActive: function(activeIndex) {
            var badgeList = this.badges, previousActiveIndex = this.currentActiveIndex;
            if (!badgeList.length) return Promise.resolve();
            var activePromises = [];
            return previousActiveIndex !== (this.currentActiveIndex = activeIndex) && badgeList[previousActiveIndex] && activePromises.push(badgeList[previousActiveIndex].setActive(false)), 
            badgeList[activeIndex] && activePromises.push(badgeList[activeIndex].setActive(true)), 
            Promise.all(activePromises);
        }
    }
});